package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.File;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.SysMessage;
import com.ruoyi.system.domain.SysReply;
import com.ruoyi.system.mapper.FileMapper;
import com.ruoyi.system.mapper.SysMessageMapper;
import com.ruoyi.system.mapper.SysReplyMapper;
import com.ruoyi.system.service.ISysReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 回复 服务层实现
 *
 * @author ruoyi
 */
@Service
public class SysReplyServiceImpl implements ISysReplyService
{
    @Autowired
    private SysReplyMapper replyMapper;

    @Autowired
    private FileMapper fileMapper;

    @Autowired
    private SysMessageMapper sysMessageMapper;


    /**
     * 查询回复信息
     *
     * @param id 回复ID
     * @return 回复信息
     */
    @Override
    public SysReply selectReplyById(Long id)
    {
        return replyMapper.selectReplyById(id);
    }

    /**
     * 查询回复列表
     *
     * @param reply 回复信息
     * @return 回复集合
     */
    @Override
    public List<SysReply> selectReplyList(SysReply reply)
    {
        return replyMapper.selectReplyList(reply);
    }

    /**
     * 新增回复
     *
     * @param reply 回复信息
     * @return 结果
     */
    @Override
    public int insertReply(SysReply reply)
    {
        int n = replyMapper.insertReply(reply);
        String fileId = reply.getFileId();
        if(StringUtils.isNotEmpty(fileId)){
            String newFileId = StringUtils.strip(fileId,"[]");
            String[] fileIds = newFileId.split(",");
            for(int i = 0 ;i < fileIds.length; i++){
                setReplyFileId(reply.getId(),Integer.parseInt(fileIds[i]));
            }
        }
        return n;
    }

    /**
     * 修改回复
     *
     * @param reply 回复信息
     * @return 结果
     */
    @Override
    public int updateReply(SysReply reply)
    {
        String fileId = reply.getFileId();
        if(StringUtils.isNotEmpty(fileId)){
            String newFileId = StringUtils.strip(fileId,"[]");
            String[] fileIds = newFileId.split(",");
            for(int i = 0 ;i < fileIds.length; i++){
                //判断fileId是否已经存在
                File file = fileMapper.selectFileById(Integer.parseInt(fileIds[i]));
                if(file == null){
                    setReplyFileId(reply.getId(),Integer.parseInt(fileIds[i]));
                }
            }
        }
        //更新message状态
        SysMessage message = new SysMessage();
        message.setId(reply.getMessageId());
        message.setMessageStatus("2");
        sysMessageMapper.updateMessage(message);
        return replyMapper.updateReply(reply);
    }

    /**
     * 删除回复对象
     *
     * @param id 回复ID
     * @return 结果
     */
    @Override
    public int deleteReplyById(Long id)
    {
        return replyMapper.deleteReplyById(id);
    }

    /**
     * 批量删除回复信息
     *
     * @param ids 需要删除的回复ID
     * @return 结果
     */
    @Override
    public int deleteReplyByIds(Long[] ids)
    {
        return replyMapper.deleteReplyByIds(ids);
    }


    //将reply的信息与文件信息绑定
    private void setReplyFileId(Long id,Integer fileId){
        File file = new File();
        file.setReplyId(id);
        file.setId(fileId);
        fileMapper.updateFile(file);
    }
}
